/**
 * 全局配置文件-公共函数
 */

/**
 * 数组对象排序方法
 * @method sortArrayByKey
 * @param {Array} list 排序数据
 * @param {String} key 排序唯一标识符
 */
export const sortArrayByKey = (array, key) => {
  return array.sort(function (a, b) {
    var x = a[key];
    var y = b[key];
    return x < y ? -1 : x > y ? 1 : 0;
  });
};
/**
 * 获取当前月份开始时间和结束时间
 * @method getCurrentMonthRange
 */

export const getCurrentMonthRange = () => {
  var nowDate = new Date();
  var endDate = getDay(-1); //当月最后一天
  var startDate = getFullDate(nowDate.setDate(1)); //当月第一天

  startDate = startDate.substring(0, 7);

  endDate = endDate.substring(0, 7);

  return {
    startTime: startDate,
    EndTime: endDate,
  };
};
/**
 * 数据去重方法
 * @method uniqueAarry
 * @param {Array} list 去重数据
 * @param {String} key 去重唯一标识符
 */
export const uniqueAarry = (list, key) => {
  const res = new Map();
  return list.filter((a) => !res.has(a[key]) && res.set(a[key], 1));
};

//百分比计算
export const percentageCalc = (type, params) => {
  if (params === 0) {
    return 0;
  } else if (params === null || params === undefined || params === "") {
    return "";
  } else {
    var paramsValue = Number(params * 100);

    if (params % 1 !== 0) {
      //保留两位小数
      paramsValue = paramsValue.toFixed(2);
    }

    if (type === "table") {
      return paramsValue + "%";
    } else {
      return paramsValue;
    }
  }
};

/**
 * 获取当前月份开始时间和结束时间
 * @method getCurrentMonthDatetime
 */

export const getCurrentMonthDatetime = () => {
  var nowDate = new Date();
  var endDate = getDay(-1); //当月最后一天
  var startDate = getFullDate(nowDate.setDate(1)); //当月第一天

  return {
    startTime: startDate,
    EndTime: endDate,
  };
};
/**
 * 获取近一个月开始时间和结束时间
 * @method getNearlyMonthDatetime
 */

export const getNearlyMonthDatetime = () => {
  var nowDate = new Date();
  var endDate = getDay(-1); //当月最后一天
  var startDate = getDay(-31); //当月第一天

  return {
    startTime: startDate,
    EndTime: endDate,
  };
};

/**
 * 获取最近七天开始时间和结束时间
 * @method getLastSevenDays
 */
export const getLastSevenDays = () => {
  //当天日期
  let startTime = getDay(-7);

  //7天前日期
  let EndTime = getDay(-1);

  return {
    startTime: startTime,
    EndTime: EndTime,
  };
};

/**
 * 获取过去半年的时间
 * @method getHalfYearFormatDate
 */
export const getHalfYearFormatDate = () => {
  var nowDate = new Date();
  var date = new Date(nowDate);

  date.setDate(date.getDate() - 184);
  var seperator1 = "-";
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  var startTime = year + seperator1 + month + seperator1 + strDate;

  //当天日期
  let EndTime = getDay(-1);

  return {
    startTime: startTime,
    EndTime: EndTime,
  };
};

/**
 * 获取过去一年的时间
 * @method getPassYearFormatDate
 */
export const getPassYearFormatDate = () => {
  var nowDate = new Date();
  var date = new Date(nowDate);
  date.setDate(date.getDate() - 366);
  var seperator1 = "-";
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  var startTime = year + seperator1 + month + seperator1 + strDate;

  //当天日期
  let EndTime = getDay(-1);

  return {
    startTime: startTime,
    EndTime: EndTime,
  };
};

/**
 * 获取完整时间格式转换
 * @method getFullDate
 */
function getFullDate(targetDate) {
  var D, y, m, d;
  if (targetDate) {
    D = new Date(targetDate);
    y = D.getFullYear();
    m = D.getMonth() + 1;
    d = D.getDate();
  }
  m = m > 9 ? m : "0" + m;
  d = d > 9 ? d : "0" + d;
  return y + "-" + m + "-" + d;
}

//计算两个日期的天数差方法
export const getDateDiff = (sDate1, sDate2) => {
  var aDate, oDate1, oDate2, iDays;
  aDate = sDate1.split("-");
  oDate1 = new Date(aDate[1] + "-" + aDate[2] + "-" + aDate[0]); //转换为12-18-2006格式
  aDate = sDate2.split("-");
  oDate2 = new Date(aDate[1] + "-" + aDate[2] + "-" + aDate[0]);
  iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24); //把相差的毫秒数转换为天数
  return iDays + 1;
};

/**
 * 获取年月日方法
 */
export const getDayDate = (date) => {
  return getDay(date);
};

/**
 * 获取年月日方法
 * @method getDay
 */
function getDay(day) {
  var today = new Date();
  var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
  today.setTime(targetday_milliseconds); //注意，这行是关键代码
  var tYear = today.getFullYear();
  var tMonth = today.getMonth();
  var tDate = today.getDate();
  tMonth = doHandleMonth(tMonth + 1);
  tDate = doHandleMonth(tDate);
  return tYear + "-" + tMonth + "-" + tDate;
}

function doHandleMonth(month) {
  var m = month;
  if (month.toString().length == 1) {
    m = "0" + month;
  }
  return m;
}

/**
 * 获取当前时间时分
 * @method getHoursMinutes
 */
export const getHoursMinutes = (date) => {
  var h, m;

  h = date.getHours();
  m = date.getMinutes();
  m = m > 9 ? m : "0" + m;
  return h + ":" + m;
};

/**
 * 获取当前时间年月日
 * @method currDateTime
 */
export const currDateTime = () => {
  var D, y, m, d;
  D = new Date();
  y = D.getFullYear();
  m = D.getMonth() + 1;
  d = D.getDate();
  m = m > 9 ? m : "0" + m;
  d = d > 9 ? d : "0" + d;
  return y + "年" + m + "月" + d + "日";
};

/**
 * 获取上一个月
 *
 * @date 格式为yyyy-mm-dd的日期，如：2014-01-25
 */
function getPreMonth(date) {
  var arr = date.split("-");
  var year = arr[0]; //获取当前日期的年份
  var month = arr[1]; //获取当前日期的月份
  var day = arr[2]; //获取当前日期的日
  var days = new Date(year, month, 0);
  days = days.getDate(); //获取当前日期中月的天数
  var year2 = year;
  var month2 = parseInt(month) - 1;
  if (month2 == 0) {
    year2 = parseInt(year2) - 1;
    month2 = 12;
  }
  var day2 = day;
  var days2 = new Date(year2, month2, 0);
  days2 = days2.getDate();
  if (day2 > days2) {
    day2 = days2;
  }
  if (month2 < 10) {
    month2 = "0" + month2;
  }
  var t2 = year2 + "-" + month2 + "-" + day2;
  return t2;
}
